﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.BasicList
{
    public partial class frm_Add_Comp : Form
    {
        private INISettings mySetting;
        private SqlConnection myConnection;
        private string error;

        public frm_Add_Comp()
        {
            InitializeComponent();

            this.mySetting = new INISettings();
            this.myConnection = new SqlConnection(mySetting.SQLConnectionString);
            this.error = null;
            this.compDate.EditValue = DateTime.Now.Date;
            LoadCustomer();          
        }

        ////////////////////////////////////////////
        /// Load Currency List
        ////////////////////////////////////////////
        private void LoadCustomer()
        {
            try
            {
                myConnection.Open();
                SqlCommand cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT CustomerID, FullName FROM [222_Customer] ORDER BY FullName";
                SqlDataReader dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Text = dataReader["FullName"].ToString();
                    item.Value = dataReader["CustomerID"].ToString();
                    this.customerID.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();
                myConnection.Close();
            }
            catch
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                MessageBox.Show("Cannot get Customer list");
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            error = null;
            string comp_id = null;
            string comp_english = null;
            string comp_vietnamese = null;            
            string customer_id = null;
            string weight = null;
            string comp_size = null;
            string robot_name = null;
            DateTime comp_date = new DateTime();
            string plan_name = null;
            string serial_no = null;
            
            ComboboxItem item = new ComboboxItem();
            
            if (string.IsNullOrEmpty((this.compID.Text)))
            {
                error = "Please enter Comp ID";
            }
            else
            {
                comp_id = this.compID.Text;
            }

            if (string.IsNullOrEmpty(error))
            {
                comp_english = this.compEnglish.Text;
                comp_vietnamese = this.compVietnamese.Text;
                if (this.customerID.SelectedIndex >= 0)
                {
                    item = (ComboboxItem)this.customerID.SelectedItem;
                    customer_id = item.Value.ToString();
                }
                if (!string.IsNullOrEmpty(this.weightValue.Text))
                {
                    float w = 0;
                    if (float.TryParse(this.weightValue.Text, out w))
                    {
                        weight = this.weightValue.Text;
                    }
                    else
                    {
                        error = "Invalid weight";
                    }
                }
                comp_size = this.compSize.Text;
                robot_name = this.robotName.Text;
                if (this.compDate.EditValue != null)
                {
                    comp_date = DateTime.Parse(this.compDate.EditValue.ToString());
                }
                plan_name = this.planName.Text;
                serial_no = this.serialNo.Text;
            }

            if (string.IsNullOrEmpty(error))
            {
                try
                {
                    myConnection.Open();
                    string sql = "INSERT INTO [222_Comp] (CompID, CompEnglish, CompVietnamese,"
                        + " CustomerID, Weight, CompSize, RobotName, CompDate, PlanName, SerialNo)"
                        + " VALUES ('" + comp_id + "', '" + comp_english + "', N'" + comp_vietnamese + "',"
                        + " '" + customer_id + "', '" + weight + "', '" + comp_size + "', '" + robot_name + "',"
                        + " @compdate, '" + plan_name + "', '" + serial_no + "')";
                    SqlCommand cmd_sql = new SqlCommand(sql, myConnection);
                    cmd_sql.Parameters.Add("@compdate", SqlDbType.DateTime).Value = comp_date.Date;
                    cmd_sql.ExecuteNonQuery();
                    cmd_sql.Dispose();
                    myConnection.Close();
                    clearForm();
                    MessageBox.Show("Comp added success.");
                }
                catch //(Exception ex)
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    //MessageBox.Show(ex.Message);
                    MessageBox.Show("Cannot add Comp. Please try again.");
                }
            }
            else
            {
                MessageBox.Show(error);
            }
        }

        private void clearForm()
        {
            foreach (Control c in this.Controls)
            {
                if (c is DevExpress.XtraEditors.TextEdit)
                {
                    ((DevExpress.XtraEditors.TextEdit)c).Text = null;
                }

                if (c is DevExpress.XtraEditors.ComboBoxEdit)
                {
                    ((DevExpress.XtraEditors.ComboBoxEdit)c).SelectedIndex = -1;
                }

                if (c is System.Windows.Forms.MaskedTextBox)
                {
                    ((System.Windows.Forms.MaskedTextBox)c).Text = null;
                }
            }
        }
    }
}
